﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HeroDream
{
    class Shop
    {
        /// <summary>
        /// 商店装备列表
        /// </summary>
        List<Equipment> equipments = new List<Equipment>();

        public Shop()
        {
            //生成5件装备
            //TODO
            Equipment equipment1 = new Weapon("无尽", 1);
            equipments.Add(equipment1);

            Equipment equipment2 = new Weapon("饮血", 2);
            equipments.Add(equipment2);

            Equipment equipment3 = new Armor("复活甲", 3);
            equipments.Add(equipment3);


            //对装备进行排序
            //  Sort();
            // InsertSort();
        //  equipments.Sort(SortUtils.Daew);
            equipments.Sort((Equipment A, Equipment B)=> 
            {
                if (A.Price < B.Price)
                {
                    return -1;
                }
                else if (A.Price > B.Price)
                {
                    return 1;
                }
                else
                {
                    return 0;
                }

            } );
        }

        public void Sort()
        {

            for (int i = 0; i < equipments.Count; i++)//对len个元素进行排序
            {
                //每次循环把最大的元素冒泡到最后边来。
                //当i为2的时候，代表我们执行完两次了，找出了最大的两个元素，把最大的两个元素放在最后边，
                //这个时候j需要从0到(总个数-已排序个数)之间进行冒泡出最大的数据来。即从0到3两两交换，把最大的元素放到3那个位置去。
                for (int j = 0; j < equipments.Count - i - 1; j++)
                {
                    if (equipments[j + 1].Price > equipments[j].Price)
                    {
                        Equipment temp = equipments[j + 1];
                        equipments[j + 1] = equipments[j];
                        equipments[j] = temp;
                    }
                }
            }
        }

        //插入排序
        /// <summary>
        /// 插入排序 时间复杂度为O(n^2）
        /// </summary>
        /// <param name="array"></param>
        /// <param name="len"></param>
        public  void InsertSort()
        {
            for (int i = 0; i < equipments.Count; i++)//对len个元素进行排序
            {
                //取出待插入的数据
                Equipment temp = equipments[i];
                //从已排序数据中（0到i-1个数据已排序了），从后向前扫描
                int j = i - 1;//定义数据j,当每个
                for (j = i - 1; j >= 0; j--)
                {
                    if (equipments[j].Price< temp.Price)//如果当前值大于待插入的值，则把当前值向后移动一位
                    {
                        equipments[j + 1] = equipments[j];
                    }
                    else//否则代表当前值小于待插入的值了，这个时候就需要把新值插入到当前值（array[j]）的后边。
                    {
                        // array[j + 1] = temp;
                        break;
                    }
                }
                equipments[j + 1] = temp;
            }
        }


        /// <summary>
        /// 获取某个装备的价格
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public int GetPrice(int index)
        {
            var buyE = equipments[index];
            return buyE.Price;
        }

        /// <summary>
        /// 购买某件装备
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public Equipment Buy(int index)
        {
            var buyE = equipments[index];
            equipments.RemoveAt(index);
            return buyE;
        }


        public void Output()
        {
            Console.WriteLine("欢迎来到商店");
            foreach(var equip in equipments)
            {
                equip.Output();
            }

        }
    }
}
